package launcher.localLauncher;

import java.awt.Color;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import model.Couleur;

import javax.swing.JFrame;

import model.ChessGame;
import controler.ChessGameControlers;
import controler.controlerLocal.ChessGameControler;
import vue.ChessGameGUI;

public class Serveur {
	private static void log(String message, int joueur)
	{
		if(joueur == 0)
		{
			System.out.println("Blanc : " + message);
		}
		else if(joueur == 1)
		{
			System.out.println("Noir : " + message);
		}
		else
		{
			System.out.println("Serveur : " + message);
		}
	}
	
	public static void envoieMessage(Socket socket, Object objet) throws IOException
	{
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
	    oos.writeObject(objet);
	    oos.flush();
	}
	
	public static void envoieMessage(Socket socket, String message, boolean isOk) throws IOException
	{
        PrintWriter out;
		String prefix = (isOk ? "OK" : "ERR");
		out = new PrintWriter(socket.getOutputStream());
        out.println(prefix + " : " + message + "\r\n");
        out.flush();
	}
	public static String lireMessage(Socket socket){
		String message;
		do
		{
			try {
				BufferedReader in;
				in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
				message = in.readLine();		
				
			} catch (IOException e) {
				e.printStackTrace();
				return "Erreur : Recpetion du message";
			}
		} while(message == null);
		
		return message;
	}
	
	public static Object lireObjet(Socket socket){
		Object message;
		do
		{
			try {
				ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
				message = ois.readObject();			
				
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				return null;
			}
		}while ( message == null);
		return message;
	}
	
	public static void main(String[] args) throws ClassNotFoundException, InterruptedException {
		ServerSocket socketserver = null ;
        BufferedReader in;
        JFrame frame;
        Socket[] socket;
        socket = new Socket[2];
        
        try {
        	log("En écoute sur le port 2009", 3);
            socketserver = new ServerSocket(2009);
	        
            for(int i = 0; i < 2;i++)
            {
            	log("En attente du joueur " + i, 3);
            	socket[i] = socketserver.accept();
            	envoieMessage(socket[i], "Connected", true);
            	System.out.println(lireMessage(socket[i]));
            	envoieMessage(socket[i], (i == 0 ? Couleur.BLANC : Couleur.NOIR));
            	System.out.println(lireMessage(socket[i]));
            	log("Le joueur s'est connecté !", i);
            }

            
            for(int i = 0; i < 2;i++)
            {
            	envoieMessage(socket[i], "Go", true);
            //	envoieMessage(socket[i], frame);
                log("Envoi du message Go", i);
            }
            
            while(true)
            {
            	
            }
	        
        }catch (IOException e) {
            e.printStackTrace();
        }
        
        try {
			for(int i = 0; i < 2;i++)
            {
            	log("Fermeture des sockets" + i, 3);
            	socket[i].close();
            }
			socketserver.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
